#include "app.H"
#include "kernel.H"

void Flash_Write(uint32_t address, uint32_t *data, uint32_t length) 
{
    FLASH_Status status;
    
    // 1. 解锁 Flash
    FLASH_Unlock();
    
    // 2. 清除标志位
    FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
    
    // 3. 擦除目标页（2KB）
    status = FLASH_ErasePage(address);
    if (status != FLASH_COMPLETE)
    {
        // 错误处理
        return;
    }
    
    // 4. 写入数据（按 32-bit 字写入）
    for (uint32_t i = 0; i < length; i++) 
	{
		FLASH_ProgramWord(address + (i * 4), data[i]);
    }
    
    // 5. 锁定 Flash
    FLASH_Lock();
}

void Flash_Read(uint32_t address, uint32_t *data, uint32_t length) 
{
    for (uint32_t i = 0; i < length; i++) 
	{
        data[i] = *(__IO uint32_t*)(address + (i * 4));
    }
}




